<template>
  <el-card class="box-card">
    <div>
      <!-- 搜索框 -->
      <el-form
        ref="searchData"
        :inline="true"
        :model="searchData"
        class="demo-form-inline"
      >
        <el-form-item label="角色名称">
          <el-input
            v-model="searchData.roleName"
            clearable
            placeholder="角色名称/备注"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            @click="getDataListBySearch"
          >查询
          </el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="openInsertWindow"
          >新增
          </el-button
          >
        </el-form-item>
      </el-form>
      <!-- 数据表单 -->
      <el-table :data="dataList" border style="width: 100%">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="编号" prop="roleId" width="55">
        </el-table-column>
        <el-table-column label="角色名称" prop="roleName" width="130">
        </el-table-column>
        <el-table-column label="创建者ID" prop="createBy" width="80">
        </el-table-column>
        <el-table-column label="创建者名称" prop="createByName" width="120">
        </el-table-column>
        <el-table-column label="活跃度" prop="activation" width="80">
          <template v-slot="scope">
            <span>{{ scope.row.byAct === "NO" ? "无法获取" : scope.row.activation }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="180">
        </el-table-column>
        <el-table-column label="备注" prop="remark" width="450">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              @click="openEditWindow(scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="deleteRole(scope.row)"
            >删除
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="openMenuWindow(scope.row.roleId)"
            >菜单
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="openPermWindow(scope.row.roleId)"
            >权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增按钮打开的页面 -->
      <el-dialog :visible.sync="insertWindow" title="新增角色">
        <el-form :model="insertData">
          <el-form-item label="名称" label-width="120px">
            <el-input
              v-model="insertData.roleName"
              autocomplete="off"
              placeholder="名称"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="角色级别" label-width="120px">
            <el-input
              v-model="insertData.access"
              autocomplete="off"
              placeholder="1级最高"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="角色活跃度" label-width="120px">
            <el-input
              v-model="insertData.activation"
              autocomplete="off"
              placeholder="角色紧急所需活跃度"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="描述信息" label-width="120px">
            <el-input
              v-model="insertData.remark"
              style="width: 80%"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="insertWindow = false">取 消</el-button>
          <el-button type="primary" @click="insertRole()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑按钮打开的页面 -->
      <el-dialog
        :visible.sync="editWindow"
        title="编辑角色"
      >
        <el-form :model="editData">
          <el-form-item label="角色名称" label-width="120px">
            <el-input
              v-model="editData.roleName"
              autocomplete="off"
              placeholder="角色名称"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="角色级别" label-width="120px">
            <el-input
              v-model="editData.access"
              autocomplete="off"
              placeholder="1级最高"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="描述信息" label-width="120px">
            <el-input
              v-model="editData.remark"
              style="width: 80%"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editWindow = false">取 消</el-button>
          <el-button type="primary" @click="updateRole()">更 新</el-button>
        </div>
      </el-dialog>
      <!-- 菜单按钮打开的页面 -->
      <el-dialog
        :visible.sync="menuWindow"
        title="编辑菜单"
      >
        <el-tree :check-strictly="true"
                 :data="AllMenu"
                 :default-checked-keys="menu"
                 :props="menuProps"
                 accordion
                 node-key="menuId"
                 show-checkbox
                 @check="updateMenu"></el-tree>
        <div slot="footer" class="dialog-footer">
          <el-button @click="menuWindow = false">取 消</el-button>
          <el-button type="primary" @click="updateMenu2()">更 新</el-button>
        </div>
      </el-dialog>
      <!-- 权限按钮打开的页面 -->
      <el-dialog
        :visible.sync="permWindow"
        title="编辑权限"
      >
        <el-tree :check-strictly="true"
                 :data="AllPerm"
                 :default-checked-keys="perm"
                 :props="permProps"
                 accordion
                 node-key="name"
                 show-checkbox
                 @check="updatePerm"></el-tree>
        <div slot="footer" class="dialog-footer">
          <el-button @click="permWindow = false">取 消</el-button>
          <el-button type="primary" @click="updatePerm2()">更 新</el-button>
        </div>
      </el-dialog>
      <!-- 页号 -->
      <el-pagination
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="totalPage"
        layout="total ,sizes,prev,pager,next,jumper"
        style="margin-top: 30px"
        @size-change="sizeChangeHandle"
        @current-change="CurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>
<script>
export default {
  name: "SysRole",
  data() {
    return {
      //数据参数
      searchData: {},//搜索数据
      editData: {}, //编辑数据
      insertData: {},//新增数据

      //集合参数
      dataList: [], //页面展示的数据集合
      AllMenu: [],//所有菜单列表
      menu: [],//菜单列表
      menu1: [],//未修改的菜单菜单列表
      AllPerm: [],//所有权限列表
      perm: [],//权限列表
      perm1: [],//未修改的权限列表

      //页号参数
      pageIndex: 1,
      pageSize: 5,
      pageSizes: [],
      totalPage: 0,
      begin: 0,
      end: this.pageSize - 1,

      //窗口显示参数
      insertWindow: false, //默认关闭新建用户界面
      editWindow: false, //默认关闭编辑用户界面
      menuWindow: false, //默认关闭菜单界面
      permWindow: false, //默认关闭权限界面

      //其他
      menuProps: {//菜单列表结构
        children: 'children',
        label: 'name'
      },
      permProps: {//菜单列表结构
        label: 'name',
      },
      roleId: 0,//当前点击的角色级别
      dataListFrom: "getDataList",//当前数据来源于搜索还是全局
    };
  },
  methods: {
    //改变数组大小
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.SizeOrCurrentChangeAfter();
    },
    //改变当前页号
    CurrentChangeHandle(val) {
      this.pageIndex = val;
      this.SizeOrCurrentChangeAfter();
    },
    //在更改数组大小或者页号前，判断数据来源
    SizeOrCurrentChangeAfter() {
      if (this.dataListFrom === "getDataList")
        this.getDataList();
      else this.getDataListBySearch();
    },

    //添加角色
    insertRole() {
      const data = {
        roleBo: this.insertData,
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
      }
      this.$http.post("/role/insert", data).then((res) => {
        this.insertWindow = false;
        this.cutDataList(res);
      });
    },
    //修改角色
    updateRole() {
      if (this.editData.access.length === 0) {
        this.$message({
          type: "error",
          message: "角色级别不能为空",
        });
      } else if (sessionStorage.getItem("roleId") > this.editData.access) {
        this.$message({
          type: "error",
          message: "当前用户的菜单不足",
        });
      } else {
        const data = {
          roleBo: this.editData,
          pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
        }
        this.$http.post("/role/update", data).then((res) => {
          this.cutDataList(res);
          this.editWindow = false;
        });
      }
    },
    //删除角色
    deleteRole(item) {
      if (sessionStorage.getItem("roleId") >= item.access) {
        this.$message({
          type: "warning",
          message: "用户无法删除高级别的角色",
        });
      } else {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            // 表示处理的是 删除操作
            const data = {
              roleBo: item,
              pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
            }
            this.$http.post("/role/delete", data).then((res) => {
              this.cutDataList(res);
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    //更改角色菜单
    updateMenu(data) {
      const id = data.menuId;
      if (this.menu.includes(id)) {
        if (data.children !== null) {
          data.children.forEach(child => {
            this.menu = this.menu.filter((item) => {
              return item !== child.menuId;
            });
          })
        }
        this.menu = this.menu.filter((item) => {
          return item !== id;
        });
      } else {
        this.menu.push(id)
        if (data.parentId !== 0) {
          this.menu.push(data.parentId)
        }
      }
      //去重
      this.menu = [...new Set(this.menu)];
    },
    //将更改后的菜单传给后端
    updateMenu2() {
      const data = {
        newMenu: this.menu,
        oldMenu: this.menu1,
        roleId: this.roleId
      }
      this.$http.post("/menu/changeRoleMenu", data);
      this.menuWindow = false;
    },
    //更改角色权限
    updatePerm(data) {
      const name = data.name;
      if (this.perm.includes(name)) {
        this.perm = this.perm.filter((item) => {
          return item !== name;
        });
      } else {
        this.perm.push(name)
      }
      //去重
      this.perm = [...new Set(this.perm)];
    },
    //将更改后的权限传给后端
    updatePerm2() {
      const data = {
        newPerm: this.perm,
        oldPerm: this.perm1,
        roleId: this.roleId
      }
      this.$http.post("/perm/changeRolePerm", data);
      this.permWindow = false;
    },

    //分页操作
    cutDataList(res) {
      const data = this.$cutPageDataList(res)
      this.dataList = data.dataList
      this.totalPage = data.totalPage
    },

    //查询所有数据
    getDataList() {
      this.dataListFrom = "getDataList";
      const pageInfo = this.$getPageInfo(this.pageSize, this.pageIndex)
      this.$http.post("/role/getPageVo", pageInfo).then((res) => {
        this.cutDataList(res);
      });
    },
    //查询单条数据
    getDataListBySearch() {
      this.dataListFrom = "getDataListBySearch";
      const data = {
        roleBo: this.searchData,
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
      }
      this.$http.post("/role/getPageVo/search", data).then((res) => {
        this.cutDataList(res);
      });
    },

    //打开编辑页面
    openEditWindow(item) {
      if (sessionStorage.getItem("roleId") >= item.access) {
        this.$message({
          type: "warning",
          message: "用户无法编辑高级别的角色",
        });
      } else {
        this.editWindow = true; //打开编辑用户界面
        //回显数据
        this.editData = this.$CopyObject(item);
      }
    },
    //打开菜单页面
    openMenuWindow(roleId) {
      this.$http.post("/menu/getListByRole/" + roleId).then((res) => {
        this.menu = res;
        this.menu1 = JSON.parse(JSON.stringify(res));
      });
      this.$http.post("/menu/getList").then((res) => {
        this.AllMenu = res;
      });
      this.roleId = roleId
      this.menuWindow = true;
    },
    //打开权限页面
    openPermWindow(roleId) {
      this.$http.post("/perm/getListByRole/" + roleId).then((res) => {
        this.perm = res;
        this.perm1 = JSON.parse(JSON.stringify(res));
      });
      this.$http.post("/perm/getList").then((res) => {
        this.AllPerm = res;
      });
      this.roleId = roleId
      this.permWindow = true;
    },
    //打开新增页面
    openInsertWindow() {
      this.insertWindow = true;
      this.insertData = {}
    },

    //初始化页号
    setPageSize() {
      this.pageSizes = this.$getPageSize()
      this.pageSize = this.pageSizes[0]
    }
  },
  mounted() {
    this.setPageSize()
    this.getDataList();
  },
};
</script>
